package cn.ekuma.pos.pda.inventory;

import android.content.Intent;
import android.view.View;

import com.openbravo.bean.Product;
import com.openbravo.bean.ProductCategory;
import com.openbravo.data.basic.BasicException;
import com.openbravo.data.loader.query.QBFCompareEnum;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import cn.ekuma.android.ui.filter.QuickTimeFilter;
import cn.ekuma.android.ui.filter.RefSelectFilter;
import cn.ekuma.data.dao.I_DataLogic;
import cn.ekuma.data.ui.android.AbstractFinderActivate;
import cn.ekuma.data.ui.android.editor.AbstractDAOJEditor;
import cn.ekuma.data.ui.android.editor.AbstractTimeCycleListPageEditorActivate;
import cn.ekuma.epos.android.finder.ProductCategoryFinderActivate;
import cn.ekuma.epos.datalogic.DataLogicPDA;
import cn.ekuma.epos.datalogic.I_DataLogicERP;
import cn.ekuma.pos.pda.inventory.editor.ProductEditor;

public class ProductEditorActivate extends AbstractTimeCycleListPageEditorActivate<Product> {
    public static final int SELECT_PRODUCTCATEGORY=3;
    private RefSelectFilter advFilter;
    ProductEditor editor;
    private ProductCategory productCategory;

	public Class getSuportClass() {
		return Product.class;
	}

    @Override
    public void activate() throws BasicException {
        quickTimeFilter.fireTodayCycle();
        quickTimeFilter.fireTimeCycleChanged();
        if(m_db.getCount()==0){
            quickTimeFilter.fireWeekCycle();
            quickTimeFilter.fireTimeCycleChanged();
        }
        if(m_db.getCount()==0){
            quickTimeFilter.fireMonthCycle();
            quickTimeFilter.fireTimeCycleChanged();
        }
    }

    @Override
	protected I_DataLogic getDataLogic() {
		return DataLogicPDA.getDLERP();
	}

	@Override
	protected AbstractDAOJEditor<Product> createEditor() {
		return new ProductEditor(dirty,this,null);
	}

    @Override
    protected void onActivityResult(int page, int requestCode, int resultCode, Intent data) {
        if(resultCode==RESULT_OK){
            switch(requestCode){
                case SELECT_PRODUCTCATEGORY:
                    productCategory=(ProductCategory) data.getSerializableExtra(AbstractFinderActivate.SELECTED_BEAN);
                    advFilter.setSelectedBeanText(productCategory.getName());
                    break;
            }
        }else{
            switch(requestCode){
                case SELECT_PRODUCTCATEGORY:
                    productCategory=null;
                    advFilter.setSelectedBeanText(null);
                    break;
            }
        }
    }

    @Override
    public String getRenderString(Object item) {
        return ((Product)item).getName();
    }

    public void filterParaReset() {
        super.filterParaReset();
        productCategory=null;
        advFilter.setSelectedBeanText(null);
    }

    @Override
    public View createAdvFilterPanel() {
        advFilter=new RefSelectFilter(this,SELECT_PRODUCTCATEGORY,"分类");
        advFilter.setRefSelectedListener(new RefSelectFilter.RefSelectedListener(){

            public void refSelected(RefSelectFilter filter, int requestCode) {
                Intent intent = new Intent();
                intent.setClass(ProductEditorActivate.this,ProductCategoryFinderActivate.class);
                startActivityForResult(intent, requestCode);
            }
        });
        return advFilter;
    }

    @Override
    public void initAdvSeachView() {
    }

    @Override
    public void timeCycleChanged(Date startdate, Date enddate) {
        Object[] afilter=(Object[])createValue();
        afilter[10] = (startdate == null) ? QBFCompareEnum.COMP_NONE : QBFCompareEnum.COMP_GREATEROREQUALS;
        afilter[11] = startdate;
        afilter[12] = (enddate == null) ? QBFCompareEnum.COMP_NONE : QBFCompareEnum.COMP_LESS;
        afilter[13] = enddate;
        query(afilter);
    }


    private void query(final Object afilter){
        MyAsyncTask myAsyncTask = new MyAsyncTask(ProductEditorActivate.this) {
          public  List<Product> query() throws Exception {
                 I_DataLogicERP dlERP=DataLogicPDA.getDLERP();
                  List<Product> lists=dlERP.getProductList(afilter);
                   if(lists==null)
                         lists=new ArrayList();
                return lists;
            };
        };
        myAsyncTask.execute(1);
    }


    public void onQuickSeach(String inputText) {
        Object[] afilter=(Object[])createValue();
        afilter[0] = QBFCompareEnum.COMP_RE;
        afilter[1] = inputText;
        //afilter[8] = QBFCompareEnum.COMP_RE;
        //afilter[9] = "%" + m_jtxtBarCode.getText() + "%";

        query(afilter);
    }

    private Object createValue(){

        Object[] afilter = new Object[14];

        // name
        afilter[0] = QBFCompareEnum.COMP_NONE;
        afilter[1] = null;

        // Precio de compra
        afilter[2]  = QBFCompareEnum.COMP_NONE;
        afilter[3] = null;

        // Precio de venta
        afilter[5] =null;
        afilter[4] = QBFCompareEnum.COMP_NONE ;

        // Categoria
        if (productCategory==null) {
            afilter[6] = QBFCompareEnum.COMP_NONE;
            afilter[7] = null;
        } else {
            afilter[6] = QBFCompareEnum.COMP_EQUALS;
            afilter[7] = productCategory.getID();
        }

        //BarCode
        afilter[8] = QBFCompareEnum.COMP_NONE;
        afilter[9] = null;


        // Date range

        afilter[10] = QBFCompareEnum.COMP_NONE ;
        afilter[11] = null;
        afilter[12] =  QBFCompareEnum.COMP_NONE ;
        afilter[13] = null;

        return afilter;
    }
}
